package CompanyLogic

import (
	"CRM_rorm/logic/StaffLogic"
	"CRM_rorm/model"
	"CRM_rorm/response"
	"CRM_rorm/service"
	"github.com/gin-gonic/gin"
)



func CompanyAdd(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.CompanyProjectModel.Insert(company)
	if err != nil {
		response.Fail(ctx, nil, "增加失败")
		return
	}
	response.Success(ctx, nil, "增加成功")
}

func CompanyDeleteSoft(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.CompanyProjectModel.DeleteSoft(company)
	if err != nil {
		response.Fail(ctx, nil, "软删除失败")
		return
	}
	response.Success(ctx, nil, "软删除成功")
}

func CompanyDelete(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.CompanyProjectModel.Delete(company)
	if err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "删除成功")
}

func CompanyFind(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	company, err = service.Svc.CompanyProjectModel.FindOne(company)
	if err != nil {
		response.Fail(ctx, nil, "查找失败")
		return
	}
	response.Success(ctx, gin.H{
		"Company_id":   company.Company_id,
		"Company_name": company.Company_name,
	}, "查询成功")

}

func CompanyUpdate(ctx *gin.Context)  {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.CompanyProjectModel.Update(company)
	if err != nil {
		response.Fail(ctx,nil,"更新失败")
		return
	}
	response.Success(ctx,nil,"更新成功")
}